{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "51f516ed-a0dd-452d-b1a4-412d68dde67c",
   "metadata": {},
   "outputs": [],
   "source": [
    "from torch.utils.data import Dataset\n",
    "from PIL import Image\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "93f3ad22-84a6-46f5-8349-016eaf98c3b4",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "class mydata(Dataset):\n",
    "    def __init__(self,root_dir,label_dir):\n",
    "        self.root_dir = root_dir\n",
    "        self.label_dir = label_dir\n",
    "        self.path = os.path.join(self.root_dir,self.label_dir)\n",
    "        self.img_path = os.listdir(self.path)\n",
    "    def __getitem__(self,idx):\n",
    "        img_name = self.img_path[idx]\n",
    "        img_item_path = os.path.join(self.path,img_name)\n",
    "        img = Image.open(img_item_path)\n",
    "        label = self.label_dir\n",
    "        return img, label\n",
    "    def __len__(self):\n",
    "        return len(self.img_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "d4b62459-9b2e-4333-8a52-3682eee1d57b",
   "metadata": {},
   "outputs": [],
   "source": [
    "root_dir = \"D:\\\\desktop\\\\practice\\\\数据集\\\\hymenoptera_data\\\\train\"\n",
    "label_dir = \"ants\"\n",
    "ants_dataset = mydata(root_dir,label_dir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7801e0e1-eb75-429c-ad40-02df920cba2a",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "00823ef9-7894-44c3-81ee-af30556a03fe",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b0dca220-05a4-4e97-8b62-ca90741903a6",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(474, 654)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ed44e828-27d6-4a43-ad98-68dbbd9e33d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "img.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e4adf511-86a2-4955-a1bb-3286cf81334b",
   "metadata": {},
   "outputs": [],
   "source": [
    "dir_path = \"D:\\\\desktop\\\\practice\\\\数据集\\\\hymenoptera_data\\\\train\\\\ants\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "eea5f9e1-fb8f-457f-8263-1582ee3e9d13",
   "metadata": {},
   "outputs": [],
   "source": [
    "image_path_list = os.listdir(dir_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "68302c24-0e10-49a9-94e3-192f3f445e4c",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0013035.jpg\n"
     ]
    }
   ],
   "source": [
    "print(image_path_list[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ebc5630e-eb3c-4917-a384-5e9a3db2f03f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
